####
#Analysis from paper that use two-way fixed effects
####

library(stargazer)
load('ownership.staff.frame.RData')

#Robust SE function

fit.w.robust <- function(model, cluster.var, dta){	
	robust.se <- function(model, cluster){
		require(sandwich)
		require(lmtest)
		M <- length(unique(cluster))
		N <- length(cluster)
		K <- model$rank
		dfc <- (M/(M - 1)) * ((N - 1)/(N - K))
		uj <- apply(estfun(model), 2, function(x) tapply(x, cluster, sum));
		rcse.cov <- dfc * sandwich(model, meat = crossprod(uj)/N)
		rcse.se <- coeftest(model, rcse.cov)
		return(list(rcse.cov, rcse.se))
	}
	
	out <- model$model
	clustervar <- mapply(paste,cluster.var,dta[!(1:dim(dta)[1] %in% model$na.action),cluster.var],sep="")
	vcov <- robust.se(model, clustervar)
	model$se <- vcov[[1]]
	model$coeftest <- vcov[[2]]
	return(model)
}

###
#Appendix Table C1: TWFE
###

main.twoway <- lm(reporters.editors.lead ~ I(owner.type=='Investment') + factor(year) + factor(name) +  circulation + home.county.circulation.share + population + white.share + median.income + bachelors.degree,data=ownership.staff.frame)
main.twoway  <- fit.w.robust(main.twoway,'name',ownership.staff.frame)

entertainment.twoway <- lm(entertainment.reporters.editors.lead ~ I(owner.type=='Investment') + factor(year) + factor(name) +  circulation + home.county.circulation.share + population + white.share + median.income + bachelors.degree,data=ownership.staff.frame)
entertainment.twoway  <- fit.w.robust(entertainment.twoway,'name',ownership.staff.frame)

general.twoway <- lm(I(reporters.editors.lead-entertainment.reporters.editors.lead) ~ I(owner.type=='Investment') + factor(year) + factor(name) +  circulation + home.county.circulation.share + population + white.share + median.income + bachelors.degree,data=ownership.staff.frame)
general.twoway  <- fit.w.robust(general.twoway,'name',ownership.staff.frame)

stargazer(main.twoway,general.twoway,entertainment.twoway,omit="name|year|circulation|population|white|median|bachelors|Intercept",omit.stat=c('rsq','adj.rsq','ser','f'),se=list(sqrt(diag(main.twoway$se)),sqrt(diag(general.twoway$se)),sqrt(diag(entertainment.twoway$se))),digits=1,intercept.bottom=FALSE,covariate.labels=c("(Intercept)","Investment Owner (t)"),star.cutoffs=c(0.05),notes.align='l',notes=c('Robust Standard Errors, Clustered by Newspaper','Models include Newspaper and Year Fixed Effects','Models control for market and audience characteristics','*p<0.05'),notes.append=FALSE,column.labels=c('All','General/Political','Entertainment'),title="Effect of Ownership Type on Newsrooms",dep.var.labels.include=FALSE,dep.var.caption=c(""),model.numbers=FALSE,no.space=TRUE)

###
#Appendix Table C2: Pre-Trends
###

#ownership.staff.frame$investment.lead[which(is.na(ownership.staff.frame$investment.lead))] <- ownership.staff.frame$investment[which(is.na(ownership.staff.frame$investment.lead))]

main.twoway.pretrend.1 <- lm(reporters.editors.lead ~ investment.lead + investment + factor(year) + factor(name) +  circulation + home.county.circulation.share + population + white.share + median.income + bachelors.degree,data=ownership.staff.frame)
main.twoway.pretrend.1  <- fit.w.robust(main.twoway.pretrend.1,'name',ownership.staff.frame)

main.twoway.pretrend.2 <- lm(reporters.editors.lead ~ investment.lead.2 + investment.lead + investment + factor(year) + factor(name) +  circulation + home.county.circulation.share + population + white.share + median.income + bachelors.degree,data=ownership.staff.frame)
main.twoway.pretrend.2  <- fit.w.robust(main.twoway.pretrend.2,'name',ownership.staff.frame)

main.twoway.pretrend.3 <- lm(reporters.editors.lead ~ investment.lead.3 + investment.lead.2 + investment.lead + investment + factor(year) + factor(name) +  circulation + home.county.circulation.share + population + white.share + median.income + bachelors.degree,data=ownership.staff.frame)
main.twoway.pretrend.3  <- fit.w.robust(main.twoway.pretrend.3,'name',ownership.staff.frame)

stargazer(main.twoway.pretrend.3,main.twoway.pretrend.2,main.twoway.pretrend.1,omit="name|year|circulation|population|white|median|bachelors",omit.stat=c('rsq','adj.rsq','ser','f'),se=list(sqrt(diag(main.twoway.pretrend.3$se)),sqrt(diag(main.twoway.pretrend.2$se)),sqrt(diag(main.twoway.pretrend.1$se))),digits=1,intercept.bottom=FALSE,covariate.labels=c("(Intercept)","Investment Owner (t+3)","Investment Owner (t+2)","Investment Owner (t+1)","Investment Owner (t)"),star.cutoffs=c(0.05),notes.align='l',notes=c('Robust Standard Errors, Clustered by Newspaper','Models include Newspaper and Year Fixed Effects','Models control for market and audience characteristics','*p<0.05'),notes.append=FALSE,column.labels=c('3 Leads','2 Leads','1 Lead'),title="Effect of Investment Ownership on Newsroom Size (Assessing Pre-Trend)",dep.var.labels.include=FALSE,dep.var.caption=c(""),model.numbers=FALSE,no.space=TRUE)

###
#Appendix Table C3: Alt Specifications
###

#Additional Specifications
main.twoway.trends <- lm(reporters.editors.lead ~ investment + factor(year) + factor(name)*year +  circulation + home.county.circulation.share + population + white.share + median.income + bachelors.degree,data=ownership.staff.frame)
main.twoway.trends  <- fit.w.robust(main.twoway.trends,'name',ownership.staff.frame)

main.twoway.papersizefe <- lm(reporters.editors.lead ~ investment + factor(year) + factor(name) + factor(year.size.fe) +  circulation + home.county.circulation.share + population + white.share + median.income + bachelors.degree,data=ownership.staff.frame)
main.twoway.papersizefe  <- fit.w.robust(main.twoway.papersizefe,'name',ownership.staff.frame)

stargazer(main.twoway.trends,main.twoway.papersizefe,omit="name|year|circulation|population|white|median|bachelors",omit.stat=c('rsq','adj.rsq','ser','f'),se=list(sqrt(diag(main.twoway.pretrend.3$se)),sqrt(diag(main.twoway.pretrend.2$se)),sqrt(diag(main.twoway.pretrend.1$se))),digits=1,intercept.bottom=FALSE,covariate.labels=c("(Intercept)","Investment Owner (t+3)","Investment Owner (t+2)","Investment Owner (t+1)","Investment Owner (t)"),star.cutoffs=c(0.05),notes.align='l',notes=c('Robust Standard Errors, Clustered by Newspaper','Models include Newspaper and Year Fixed Effects','Models control for market and audience characteristics','*p<0.05'),notes.append=FALSE,column.labels=c('3 Leads','2 Leads','1 Lead'),title="Effect of Investment Ownership on Newsroom Size (Assessing Pre-Trend)",dep.var.labels.include=FALSE,dep.var.caption=c(""),model.numbers=FALSE,no.space=TRUE)

###
#Appendix Table C4: Alt Specifications + Chain Ownership
###

#Chain
main.twoway.chain <- lm(reporters.editors.lead ~ investment + chain.ownership + factor(year) + factor(name) +  circulation + home.county.circulation.share + population + white.share + median.income + bachelors.degree,data=ownership.staff.frame)
main.twoway.chain  <- fit.w.robust(main.twoway.chain,'name',ownership.staff.frame)

stargazer(main.twoway.chain,omit="name|year|circulation|population|white|median|bachelors",omit.stat=c('rsq','adj.rsq','ser','f'),se=list(sqrt(diag(main.twoway.chain$se))),digits=1,intercept.bottom=FALSE,covariate.labels=c("(Intercept)","Investment Owner","Chain Owner"),star.cutoffs=c(0.05),notes.align='l',notes=c('Robust Standard Errors, Clustered by Newspaper','Model includes Newspaper and Year Fixed Effects','Model controls for market and audience characteristics','*p<0.05'),notes.append=FALSE,column.labels=c('Adding Chain Ownership'),title="Effect of Investment Ownership on Newsroom Size",dep.var.labels.include=FALSE,dep.var.caption=c(""),model.numbers=FALSE,no.space=TRUE)

###
#Appendix Table C5: Removing Newspapers Purchased Out of Bankruptcy
###

nobankruptcy <- subset(ownership.staff.frame,ownership.staff.frame$bankruptcy.purchase==0)
main.nobankruptcy <- lm(reporters.editors.lead ~ investment + factor(year) + factor(name) +  circulation + home.county.circulation.share + population + white.share + median.income + bachelors.degree,data=nobankruptcy)
main.nobankruptcy  <- fit.w.robust(main.nobankruptcy,'name',nobankruptcy)

stargazer(main.nobankruptcy,omit="name|year|circulation|population|white|median|bachelors",omit.stat=c('rsq','adj.rsq','ser','f'),se=list(sqrt(diag(main.nobankruptcy$se))),digits=1,intercept.bottom=FALSE,covariate.labels=c("(Intercept)","Investment Owner","Chain Owner"),star.cutoffs=c(0.05),notes.align='l',notes=c('Robust Standard Errors, Clustered by Newspaper','Model includes Newspaper and Year Fixed Effects','Model controls for market and audience characteristics','*p<0.05'),notes.append=FALSE,column.labels=c('No Investment Bankruptcy Purchases'),title="Effect of Investment Ownership on Newsroom Size",dep.var.labels.include=FALSE,dep.var.caption=c(""),model.numbers=FALSE,no.space=TRUE)

###
#Appendix Table C6: Altering Investment Definition
###

main.twoway.broad <- lm(reporters.editors.lead ~ investment.broad + factor(year) + factor(name) +  circulation + home.county.circulation.share + population + white.share + median.income + bachelors.degree,data=ownership.staff.frame)
main.twoway.broad  <- fit.w.robust(main.twoway.broad,'name',ownership.staff.frame)

nowapo <- subset(ownership.staff.frame,ownership.staff.frame$name!='Washington Post')
main.twoway.broad.nowapo <- lm(reporters.editors.lead ~ investment.broad + factor(year) + factor(name) +  circulation + home.county.circulation.share + population + white.share + median.income + bachelors.degree,data=nowapo)
main.twoway.broad.nowapo  <- fit.w.robust(main.twoway.broad.nowapo,'name',nowapo)

stargazer(main.twoway.broad,main.twoway.broad.nowapo,omit="name|year|circulation|population|white|median|bachelors",omit.stat=c('rsq','adj.rsq','ser','f'),se=list(sqrt(diag(main.twoway.broad$se)),sqrt(diag(main.twoway.broad.nowapo$se))),digits=1,intercept.bottom=FALSE,covariate.labels=c("(Intercept)","Investment Owner","Investment Owner (Broad)"),star.cutoffs=c(0.05),notes.align='l',notes=c('Robust Standard Errors, Clustered by Newspaper','Models include Newspaper and Year Fixed Effects','Models control for market and audience characteristics','*p<0.05'),notes.append=FALSE,column.labels=c('Broader Definition','Broad Definition - No WaPo'),title="Effect of Investment Ownership on Newsroom Size",dep.var.labels.include=FALSE,dep.var.caption=c(""),model.numbers=FALSE,no.space=TRUE)
